package com.hope.mychat.core;

import com.hope.mychat.core.rule.Chat;
import com.hope.mychat.core.rule.Ping;
import com.hope.mychat.domain.vo.Message;
import com.hope.mychat.support.CmdType;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.EncodeException;
import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author ZWH
 */
@Slf4j
public class MyChat {
    /**
     * 存储 websocket session
     */
    public static final Map<String, Session> LIVING_SESSIONS_CACHE = new ConcurrentHashMap<>();

    /**
     * 在线人数
     */
    public static long ONLINE_COUNT = 0;

    /**
     * 消息处理
     * @param message
     * @throws IOException
     * @throws EncodeException
     */
    public static void messageHandle(Message message) throws IOException, EncodeException {
        if (CmdType.CMD_TYPE_CHAT.getValue() == message.getCmdType()) {
            // 聊天
            Chat chat = new Chat(message);
            chat.handle();
        } else if (CmdType.CMD_TYPE_PING.getValue() == message.getCmdType()) {
            // 心跳
            Ping ping = new Ping(message);
            ping.handle();
        } else {
            log.error("错误的接收类型 {}", message.getCmdType());
            return;
        }
    }
}
